﻿using System;
using System.Data;
using Model;
namespace Dal
{
	public class GGFalshDal
	{
		public GGFalshDal()
		{
		}
		/// <summary>
		/// 读取时间空为"1900-01-01"
		/// </summary>
		/// <param name="dt">时间</param>
		/// <returns>返回时间</returns>
		private DateTime ReadTime(DateTime dt)
		{
			DateTime refdt = Convert.ToDateTime("1900-01-01");
			if (!string.IsNullOrEmpty(dt.ToString()) && dt.Year != 1)
			{
				refdt = dt;
			}
			return refdt;
		}
		 		#region Insert
 		public static int Insert(GGFalsh ggfalsh)
		{	
			string sql = @"Insert Into GGFalsh (				GGClassName,				GGTitle,				GGPhotoURL,				GGLinkURL,				GGOrderby
				) values('{0}','{1}','{2}','{3}',{4})";
			sql = string.Format(sql,				SqlHelper.String(ggfalsh.GGClassName),				SqlHelper.String(ggfalsh.GGTitle),				SqlHelper.String(ggfalsh.GGPhotoURL),				SqlHelper.String(ggfalsh.GGLinkURL),				ggfalsh.GGOrderby
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(GGFalsh ggfalsh)
		{	
			string sql = @"Update GGFalsh set 				GGClassName='{0}',				GGTitle='{1}',				GGPhotoURL='{2}',				GGLinkURL='{3}',				GGOrderby={4}				where				GGID={5}";
			sql = string.Format(sql,				SqlHelper.String(ggfalsh.GGClassName),				SqlHelper.String(ggfalsh.GGTitle),				SqlHelper.String(ggfalsh.GGPhotoURL),				SqlHelper.String(ggfalsh.GGLinkURL),				ggfalsh.GGOrderby,				ggfalsh.GGID);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 GGID)
		{
		
			string sql = @"Delete from GGFalsh where 
					GGID= {0}";
			sql = string.Format(sql,
					GGID);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from GGFalsh a "; 
			return SqlHelper.Select(sql);
		}
				
		public static GGFalsh Select(Int32 GGID)
		{
			GGFalsh ggfalsh = null;
			string sql = @"select a.* from GGFalsh a  where
					a.GGID= {0}";
			sql = string.Format(sql,
					GGID);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				ggfalsh = new GGFalsh(ds.Tables[0].Rows[0]);					
			return ggfalsh;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from GGFalsh a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from GGFalsh a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from GGFalsh a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}